"""Added ProductImage model

Revision ID: cc10ef70bf60
Revises: 6da6834b9efb
Create Date: 2024-02-29 09:41:58.100106

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "cc10ef70bf60"
down_revision = "6da6834b9efb"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "product_image",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("product_id", sa.Integer(), nullable=False),
        sa.Column("image_path", sa.String(), nullable=False),
        sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=True),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.text("now()"), nullable=True),
        sa.ForeignKeyConstraint(["product_id"], ["product.id"], name=op.f("fk_product_image_product_id_product")),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_product_image")),
    )
    connection = op.get_bind()
    result = connection.execute(sa.text("SELECT id, image_path FROM product"))
    for row in result:
        product_id, image_path = row
        connection.execute(
            sa.text("INSERT INTO product_image (product_id, image_path) VALUES (:product_id, :image_path)"),
            {"product_id": product_id, "image_path": image_path},
        )
    op.drop_column("product", "image_path")
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column("product", sa.Column("image_path", sa.VARCHAR(), autoincrement=False, nullable=False))
    op.drop_table("product_image")
    # ### end Alembic commands ###
